'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 叛军来袭
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var mongoose = require('mongoose');
var rewardItemSchema = require('./survive/rewardItemSchema');
var surviveItemSchema = require('./survive/surviveItemSchema');
var surviveMgr = require('../mgr/surviveMgr');

/**
 * 叛军来袭
 */
var surviveSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 当前波数
    current_wave: {
        type: Number,
        default: 0
    },

    // 是否结束
    is_end: {
        type: Number,
        default: 0
    },

    reward_list: [rewardItemSchema],

    // 通关的最大波数
    history_max_wave: {
        type: Number,
        default: 0
    },

    // 通关的最大波数
    max_wave: {
        type: Number,
        default: 0
    },
});

surviveSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 获取当前的波数
 * @return {[type]} [description]
 */
surviveSchema.methods.getCurrentWave = function () {
    return this.current_wave;
};

/**
 * 通关
 * @return {[type]} [description]
 */
surviveSchema.methods.passCurrentWave = function () {
    this.current_wave++;
    this.max_wave = Math.max(
        this.current_wave,
        this.max_wave
    );

    this.history_max_wave = Math.max(
        this.max_wave,
        this.history_max_wave
    );
    return this.current_wave;
};

/**
 * 直接设置为最大波数
 * @return {[type]} [description]
 */
surviveSchema.methods.passToMaxWave = function () {
    this.current_wave = this.max_wave;
    return this.current_wave;
};

/**
 * 获取最大的波数
 * @return {[type]} [description]
 */
surviveSchema.methods.getMaxWave = function () {
    return this.max_wave;
};

/**
 * 设置结束
 * @return {Boolean} [description]
 */
surviveSchema.methods.setEnd = function () {
    this.is_end = 1;
};

/**
 * 是否结束
 * @return {Boolean} [description]
 */
surviveSchema.methods.isEnd = function () {
    return this.is_end;
};

/**
 * 记录获取的奖励
 * @return {Boolean} [description]
 */
surviveSchema.methods.addRewardRecord = function (current_wave, reward_list) {
    this.reward_list.push({
        wave: current_wave,
        wave_reward_list: reward_list
    });
};

/**
 * 获取获取的列表
 * @return {Boolean} [description]
 */
surviveSchema.methods.getRewardRecord = function () {
    var reward_list = [];
    _.each(this.reward_list, function (reward_item) {
        reward_list.push({
            wave: reward_item.wave,
            reward_list: reward_item.wave_reward_list
        });
    });
    return reward_list;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
surviveSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送
 * @return {[type]} [description]
 */
surviveSchema.methods.push = function () {
    this.pushModify();
};

/**
 * 推送修改
 * @return {[type]} [description]
 */
surviveSchema.methods.pushModify = function () {
    surviveMgr.sendSurviveChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send survive message error: %j, fail ids: %j', err, fails);
                return;
            }
        });
};

if (!surviveSchema.options.toJSON) {
    surviveSchema.options.toJSON = {};
}
/* jshint unused:false */
surviveSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.role_id;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Survive', surviveSchema);
